package com.micro.school.order.entity;

import com.micro.school.order.vo.PayStudentFeeVo;
import lombok.Data;

import java.util.List;

@Data
public class GenerateBillEntity {
   private PayStudentFee studentFee;
   private List<PayStudentFeeItem> feeItems;

   public void setAmount(){
    if(null==feeItems || feeItems.isEmpty()){
        return;
    }
    Double amountTotal = Double.valueOf(0.00);
    for(PayStudentFeeItem feeVo:feeItems){
        amountTotal = safeAdd(amountTotal, feeVo.getTotalAmount());
    }
    studentFee.setAmountPayable(amountTotal);
    studentFee.setAmountPending(amountTotal);
   }
    private Double safeAdd(Double d1, Double d2) {
        d1 = d1 == null ? 0.0 : d1;
        d2 = d2 == null ? 0.0 : d2;
        return d1 + d2;
    }
   public void setFeeItemFeeId(){
       if(null==feeItems || feeItems.isEmpty()){
           return;
       }
       Integer studentFeeId = studentFee.getId();
       if(null==studentFeeId){
           return;
       }
       for(PayStudentFeeItem feeItem:feeItems){
           feeItem.setStudentFeeId(studentFeeId);
       }
   }
    public void setTenantId(){
        if(null==feeItems || feeItems.isEmpty()){
            return;
        }
        Integer studentFeeId = studentFee.getId();
        if(null==studentFeeId){
            return;
        }
        for(PayStudentFeeItem feeItem:feeItems){
            feeItem.setTenantId(studentFee.getTenantId());
        }
    }

   public PayStudentFeeVo match(List<PayStudentFeeVo> vos){
       if(null==vos || vos.isEmpty()){
           return null;
       }
       for(PayStudentFeeVo feeVo:vos){
           boolean isYearEq = null!=studentFee.getFeeYearId() && null!=feeVo.getFeeYearId() && studentFee.getFeeYearId().intValue()==feeVo.getFeeYearId();
           boolean isStudentEq = null!=studentFee.getStudentId() && null!=feeVo.getStudentId() && studentFee.getStudentId().intValue()==feeVo.getStudentId().intValue();
           if(isYearEq && isStudentEq){
               return feeVo;
           }
       }
       return null;
   }
}
